<?php use_stylesheet('jquery.autocompleter.css') ?>
<?php use_javascript('jquery.autocompleter.js') ?>
<?php use_javascript('jquery.validate.min.js') ?>
<h2>Formulario de Preinscripción</h2>
<form id="preinscripcion_form" action="<?php echo url_for('alumno/completarPreinscripcion') ?>" method="post">
    <?php if (!$form->getObject()->isNew()): ?>
        <input type="hidden" name="sf_method" value="put" />
    <?php endif; ?>
    <table class="table-form">
        <tbody>
            <tr>
                <td>
                    <label for="preinscripto_nombre" class="required">Nombre Completo</label>
                    <?php echo $form['nombre'] ?>
                    <?php echo $form['_csrf_token'] ?>
                </td>
                <td>
                    <label for="preinscripto_apellido" class="required">Apellido</label>
                    <?php echo $form['apellido'] ?>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="preinscripto_documento" class="required">N° Documento</label>
                    <?php echo $form['documento'] ?>
                </td>
                <td>
                    <label for="preinscripto_localidad_id" class="required">Localidad</label>
                    <?php echo $form['localidad_id'] ?>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="preinscripto_telefono">Teléfono</label>
                    <?php echo $form['telefono'] ?>
                </td>
                <td>
                    <label for="preinscripto_celular">Celular</label>
                    <?php echo $form['celular'] ?>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="preinscripto_email" class="required">Email</label>
                    <?php echo $form['email'] ?>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="preinscripto_unidad_formacion_id" class="required">Carrera</label>
                    <?php echo $form['unidad_formacion_id'] ?>
                </td>
                <td>
                    <label for="preinscripto_accion_comunicacional_id" class="required">Como te enteraste de la carrera?</label>
                    <?php echo $form['accion_comunicacional_id'] ?>
                </td>
            </tr>
        </tbody>
    </table><br />
    <input id="send_form" type="submit" class="btn btn-large btn-success" value="Preinscribirse" />
</form>
<script>
    $(function() {

        $('label.required').each(function() {
            var value = $(this).html();
            $(this).html(value + ' <span class="required">*</span>');
        });

        jQuery.validator.addMethod("autocomplete_required", function(value, element) {
            var ac_id = $('#' + element.id).attr('id');
            ac_id = ac_id.replace('autocomplete_', '');
            return (value != '' && $('#' + ac_id).val() != '');
        }, "Seleccione un elemento de la lista.");

        jQuery.validator.addMethod("document_unique", function(value, element) {
            return $.ajax({
                url: '<?php echo url_for('alumno/validateUniqueDocument') ?>',
                type: 'POST',
                async: false,
                data: { document: value },
                dataType: 'text',
                success: function(data) {
                    return data;
                }
            }).responseText !== 'true';
        }, "Este documento ya ha sido registrado.");

        jQuery.validator.addMethod("email_unique", function(value, element) {
            return $.ajax({
                url: '<?php echo url_for('alumno/validateUniqueEmail') ?>',
                type: 'POST',
                async: false,
                data: { email: value },
                dataType: 'text',
                success: function(data) {
                    return data;
                }
            }).responseText !== 'true';
        }, "Este email ya ha sido registrado.");

        $('#preinscripcion_form').validate({ onkeyup: false });

        $('[id$=apellido]').rules('add', {
            required: true,
            maxlength: 100
        });
        $('[id$=nombre]').rules('add', {
            required: true,
            maxlength: 100
        });
        $('[id$=documento]').rules('add', {
            required: true,
            number: true,
            maxlength: 8,
            document_unique: true
        });
        $('[id$=localidad_id][id^=autocomplete]').rules('add', {
            required: true,
            autocomplete_required: true
        });
        $('[id$=telefono]').rules('add', {
            maxlength: 15
        });
        $('[id$=celular]').rules('add', {
            maxlength: 15
        });
        $('[id$=email]').rules('add', {
            required: true,
            email: true,
            maxlength: 100,
            email_unique: true
        });

    });
</script>